﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.WebNemo.Business;
using blackhouse.WebNemo.Business.Groups;
using System.Globalization;
using System.Data;

namespace blackhouse.NemoBlog.Business {
    public class NemoBlogPosts {

        #region Fields

        internal const string GROUP_POSTS = "BLOG_POSTS";
        private static IGroup groupPosts;
        private int placeId;

        #endregion

        #region Constructors

        public NemoBlogPosts() : this(0) { }

        public NemoBlogPosts(int placeId) {
            this.placeId = placeId;
        }

        #endregion

        #region Methods

        public static IGroup BelongToGroup() {
            if (groupPosts == null)
                groupPosts = Factory.Instance().GetGroup(GROUP_POSTS);
            return groupPosts;
        }

        public NemoBlogPost[] GetLastPosts(int postCount, bool onlyPublished) {
            return this.GetLastPosts(0, postCount, onlyPublished);
        }

        public NemoBlogPost[] GetLastPosts(int lastLoadedPostId, int postCount, bool onlyPublished) {
            return GetLastPosts(placeId, lastLoadedPostId, postCount, onlyPublished);
        }

        public static NemoBlogPost[] GetLastPosts(int placeId, int lastLoadedPostId, int postCount, bool onlyPublished) {
            IGroup group = BelongToGroup();
            GroupRecords records = new GroupRecords(group);
            GroupRecordQuery q = new GroupRecordQuery(group);
            q.SetProcedureName("bhp_NemoBlogPosts_GetLast");
            q.AddProcedureParameter("Count", postCount);
            q.AddProcedureParameter("ModuleId", placeId, System.Data.DbType.Int32);
            q.AddProcedureParameter("OnlyPublic", onlyPublished, System.Data.DbType.Boolean);
            q.AddProcedureParameter("Language", CultureInfo.CurrentCulture.Name, System.Data.DbType.AnsiStringFixedLength);
            q.AddProcedureParameter("LastPostId", lastLoadedPostId, System.Data.DbType.Int32);
            return records.GetRecords<NemoBlogPost>(q);
        }

        public NemoBlogPost[] GetLastPosts(int postCount, DateTime publishedFrom, DateTime publishedTo) {
            return this.GetLastPosts(0, postCount, publishedFrom, publishedTo);
        }

        public NemoBlogPost[] GetLastPosts(int lastLoadedPostId, int postCount, DateTime publishedFrom, DateTime publishedTo) {
            return GetLastPosts(placeId, lastLoadedPostId, postCount, publishedFrom, publishedTo);
        }

        public static NemoBlogPost[] GetLastPosts(int placeId, int lastLoadedPostId, int postCount, DateTime publishedFrom, DateTime publishedTo) {
            IGroup group = BelongToGroup();
            GroupRecords records = new GroupRecords(group);
            GroupRecordQuery q = new GroupRecordQuery(group);
            q.SetProcedureName("bhp_NemoBlogPosts_GetLast");
            q.AddProcedureParameter("Count", postCount);
            q.AddProcedureParameter("ModuleId", placeId, System.Data.DbType.Int32);
            q.AddProcedureParameter("Language", CultureInfo.CurrentCulture.Name, System.Data.DbType.AnsiStringFixedLength);
            q.AddProcedureParameter("LastPostId", lastLoadedPostId, System.Data.DbType.Int32);
            q.AddProcedureParameter("FromDate", publishedFrom, System.Data.DbType.DateTime);
            q.AddProcedureParameter("ToDate", publishedTo, System.Data.DbType.DateTime);
            return records.GetRecords<NemoBlogPost>(q);
        }

        public static NemoBlogPost[] GetPosts(int placeId, bool onlyPublished) {
            IGroup group = BelongToGroup();
            GroupRecords records = new GroupRecords(group);
            GroupRecordQuery q = new GroupRecordQuery(group);
            q.SetProcedureName("bhp_NemoBlogPosts_GetAll");
            q.AddProcedureParameter("OnlyPublic", onlyPublished, System.Data.DbType.Boolean);
            q.AddProcedureParameter("ModuleId", placeId, System.Data.DbType.Int32);
            return records.GetRecords<NemoBlogPost>(q);
        }

        public NemoBlogPost[] GetPostsByTag(int tagId, bool onlyPublished) {
            return GetPostsByTagId(tagId, onlyPublished);
        }

        public static NemoBlogPost[] GetPostsByTagId(int tagId, bool onlyPublished) {
            IGroup group = BelongToGroup();
            GroupRecords records = new GroupRecords(group);
            GroupRecordQuery q = new GroupRecordQuery(group);
            q.SetProcedureName("bhp_NemoBlogPosts_GetByTag");
            q.AddProcedureParameter("OnlyPublic", onlyPublished, System.Data.DbType.Boolean);
            q.AddProcedureParameter("TagId", tagId, System.Data.DbType.Int32);
            return records.GetRecords<NemoBlogPost>(q);
        }

        /// <summary>
        /// Zwraca listę najbardziej podobnych postów
        /// </summary>
        /// <param name="postId">Id posta dla którego mamy znaleźć podobne posty</param>
        /// <returns></returns>
        public static NemoBlogPost[] GetSimilarPosts(int postId, int similarPostCount, int similarPostsDayAgo, int similarPostsForwardDays) {
            IGroup group = BelongToGroup();
            GroupRecords records = new GroupRecords(group);
            GroupRecordQuery q = new GroupRecordQuery(group);
            q.SetProcedureName("bhp_NemoBlogPosts_GetSimilars");
            q.AddProcedureParameter("PostId", postId, DbType.Int32);
            q.AddProcedureParameter("Count", similarPostCount, DbType.Int32);
            q.AddProcedureParameter("PastDays", similarPostsDayAgo, DbType.Int32);
            q.AddProcedureParameter("ForwardDays", similarPostsForwardDays, DbType.Int32);
            q.AddProcedureParameter("Language", CultureInfo.CurrentCulture.Name, DbType.AnsiStringFixedLength);
            return records.GetRecords<NemoBlogPost>(q);
        }

        #endregion

    }
}
